package com.authine.cloudpivot.controller;


import com.authine.cloudpivot.engine.api.facade.BizObjectFacade;
import com.authine.cloudpivot.engine.api.model.runtime.BizObjectCreatedModel;
import com.authine.cloudpivot.engine.api.model.runtime.BizObjectModel;
import com.authine.cloudpivot.foundation.orm.api.dml.JPANativeQuery;
import com.authine.cloudpivot.util.Result;
import com.authine.cloudpivot.web.api.controller.base.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.internal.asn1.cms.GCMParameters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/jzsq")
@Slf4j
public class JzsqController extends BaseController {


    private static final String SCHEMACODE = "concentrateDemand12345";

    @Autowired
    private BizObjectFacade bizObjectFacade;

    @Autowired
    private JPANativeQuery jpaNativeQuery;

    @PersistenceContext
    private EntityManager entityManager;



    @PostMapping("/margejzsq")
    @Transactional
    public Result margejzsq(@RequestBody List<String> ids) {
        int total = 0;
        StringBuffer wtlx = new StringBuffer();
        StringBuffer gdh = new StringBuffer();
        String id = ids.get(0);
        BizObjectCreatedModel bizObject = bizObjectFacade.getBizObject(SCHEMACODE, id);
        Map<String, Object> data = bizObject.getData();
        String totalDemand =(String) data.get("totalDemand");
        int i1 = Integer.parseInt(totalDemand);
        total+=i1;
        wtlx.append(data.get("questionTypeDistribution"));
        String caseNumber =(String) data.get("caseNumber");
        gdh.append(caseNumber);
        for (int i=1;i<ids.size();i++){
            String nextid = ids.get(i);
            BizObjectCreatedModel obj = bizObjectFacade.getBizObject(SCHEMACODE, nextid);
            Map<String, Object> data1 = obj.getData();
            wtlx.append(data1.get("questionTypeDistribution"));
            String totalDemand1 =(String) data1.get("totalDemand");
            int i2 = Integer.parseInt(totalDemand1);
            total+=i2;
            String caseNumber1 =(String) data1.get("caseNumber");
            gdh.append(",").append(caseNumber1);
            data1.put("dataStatus","废弃");
            String updateSql = String.format("update ic9na_concentratedemand12345 set dataStatus = '废弃' where id = '%s'",nextid);
            entityManager.createNativeQuery(updateSql).executeUpdate();
        }
        String result = Arrays.stream(gdh.toString().split(","))
                .distinct()
                .collect(Collectors.joining(","));
        String sql = String.format("update ic9na_concentratedemand12345 set totalDemand = '%s',questionTypeDistribution = '%s',caseNumber = '%s' where id = '%s'",total,wtlx,result,id);
        entityManager.createNativeQuery(sql).executeUpdate();
//
//        String[] oldCase = caseNumber.split(",");
//        String[] newCase = result.split(",");
//
//        Set<String> setA = new HashSet<>(Arrays.asList(oldCase));
//        // 使用 Stream 过滤出 b 中不在 a 的元素
//        List<String> diff = Arrays.stream(newCase)
//                .filter(e -> !setA.contains(e))
//                .collect(Collectors.toList());
        return Result.ok();
    }
}
